package com.hnjfzn.system.controller;

import com.hnjfzn.common.core.constant.UserConstants;
import com.hnjfzn.common.core.utils.AreaUtil;
import com.hnjfzn.common.core.utils.StringUtils;
import com.hnjfzn.common.core.web.controller.BaseController;
import com.hnjfzn.common.core.web.domain.AjaxResult;
import com.hnjfzn.common.log.annotation.Log;
import com.hnjfzn.common.log.enums.BusinessType;
import com.hnjfzn.common.security.annotation.RequiresPermissions;
import com.hnjfzn.common.security.utils.SecurityUtils;
import com.hnjfzn.system.api.domain.SysArea;
import com.hnjfzn.system.api.domain.SysDept;
import com.hnjfzn.system.service.ISysAreaService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/area")
public class SysAreaController extends BaseController {

    @Autowired
    private ISysAreaService sysAreaService;

    @RequiresPermissions("system:area:list")
    @GetMapping("/list")
    public AjaxResult list(SysArea area)
    {
        return success(sysAreaService.list(area));
    }

    @RequiresPermissions("system:area:list")
    @GetMapping("/list/exclude/{areaId}")
    public AjaxResult excludeChild(@PathVariable(value = "areaId", required = false) String areaId)
    {
        Map<String, Object> map = new HashMap<>(2);
        map.put("exAreaId", areaId);
        map.put("exLikeAreaId", AreaUtil.getFuzzyQuery(areaId));
        SysArea area = new SysArea();
        area.setParams(map);
        return success(list(area));
    }

    @RequiresPermissions("system:area:query")
    @GetMapping(value = "/{areaId}")
    public AjaxResult getInfo(@PathVariable String areaId)
    {
        sysAreaService.checkDataScope(areaId);
        return success(sysAreaService.getById(areaId));
    }

    @RequiresPermissions("system:area:add")
    @Log(title = "区域管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysArea area)
    {
        area.setCreateBy(SecurityUtils.getUsername());
        area.setCreateTime(new Date());
        return toAjax(sysAreaService.save(area));
    }

    @RequiresPermissions("system:area:edit")
    @Log(title = "区域管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysArea area)
    {
        String areaId = area.getAreaId();
        sysAreaService.checkDataScope(areaId);
        if (area.getParentId().equals(areaId))
        {
            return error("修改区域'" + area.getAreaName() + "'失败，上级区域不能是自己");
        }
        else if (StringUtils.equals(UserConstants.AREA_DISABLE, area.getStatus()) && sysAreaService.existsNormalChild(areaId))
        {
            return error("该部门包含未停用的子部门！");
        }
        area.setUpdateBy(SecurityUtils.getUsername());
        area.setUpdateTime(new Date());
        return toAjax(sysAreaService.updateById(area));
    }

    /**
     * 删除部门
     */
    @RequiresPermissions("system:area:remove")
    @Log(title = "区域管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{areaId}")
    public AjaxResult remove(@PathVariable String areaId)
    {
        if (sysAreaService.existsChild(areaId))
        {
            return warn("存在下级区域,不允许删除");
        }
        if (sysAreaService.checkExistUser(areaId))
        {
            return warn("区域存在用户,不允许删除");
        }
        sysAreaService.checkDataScope(areaId);
        return toAjax(sysAreaService.removeArea(areaId));
    }
}
